<?php
include_once('admin.inc.php');
$artModel = new Model('article');
$advModel = new Model('advertise');
$smarty = new Tpl();

$act = get_param('do');
$cid = get_param('cid');

$catModel = new Model('category');
$tmp = $catModel->get();
foreach( (array)$tmp as $v ){
	$cats[$v['id']] = $v;
}
switch($act){
	default:
	case 'list':
		$list = $artModel->order('addtime DESC')->get();
		$smarty->assign('list', $list);
		$smarty->display('admin/articles.html');
	break;
	case 'add':
		$smarty->assign('cats', $cats);
		$smarty->display('admin/addArticle.html');
	break;
	case 'addArt':
		$data = get_data();
		if( is_uploaded_file($_FILES['advpic']['tmp_name']) ){
			$upCfg = array(
				// 文件大小：3M	
				'max_size'	=> 3*1024*1024,
				'safe_ext'	=> array('jpg','jpeg','png','gif','bmp','tiff'),
				'save_path'	=> __UPLOAD__.'advertise/'
				);
			$upSaveName = 'adv_5_';
			require_once(__LIB__.'Upload.class.php');
			$up = new Upload('advpic',$upCfg);
			$rsUp = $up->save($upSaveName);
			if( !$rsUp ){	
				msg('添加失败,文件上传失败,'.$up->get('error'),-1);
			}
		}
		$data['addtime'] = time();
		if( empty($data['title']) || empty($data['category']) || empty($data['content']) ){
			dump($data);
			msg('必填项不能为空', -1);
			exit;
		}
		$reId = $artModel->value($data)->add();
		$from = array(1=>'news',2=>'zbhl');
		if( $reId>0 ){
			if( $rsUp ){
				$rsUp = json_decode($rsUp, true);
				$dataAdv = array(
					'file' => $rsUp[0]['saveName'],
					'type' => 5,
					'link' => $data['link'],
					'subtitle' => $reId,
					);
				$advModel->value($dataAdv)->add();
			}
			msg('添加成功','article.php?do='.$from[$data['category']]);
		}else{
			msg('添加失败,写入数据库失败',-1);
		}
	break;
	case 'editArt':
		$id = intval(get_param('id'));
		if( empty($id) ) msg('非法id');
		if( get_param('submit') ){
			$data = get_data();
			if( is_uploaded_file($_FILES['advpic']['tmp_name']) ){
				$upCfg = array(
					// 文件大小：3M	
					'max_size'	=> 3*1024*1024,
					'safe_ext'	=> array('jpg','jpeg','png','gif','bmp','tiff'),
					'save_path'	=> __UPLOAD__.'advertise/'
				);
				$upSaveName = 'adv_4_';
				require_once(__LIB__.'Upload.class.php');
				$up = new Upload('advpic',$upCfg);
				$rsUp = $up->save($upSaveName);
				if( !$rsUp ){
					msg('修改失败,文件上传失败,'.$up->get('error'),-1);
				}
				$rsUp = json_decode($rsUp, true);
				$dataAdv = array(
					'file' => $rsUp[0]['saveName'],
					'type' => 5,
					'link' => $data['link'],
					'subtitle' => $id,
					);
				$advModel->value($dataAdv)->add();
			}
			$data['addtime'] = time();
			if( empty($data['title']) || empty($data['category']) || empty($data['content']) ){
				msg('必填项不能为空', -1);
			}
			$rs = $artModel->where("id=$id")->value($data)->update();
			$from = array(1=>'news');
			if( $rs ){
				msg('修改成功','article.php?do='.$from[$data['category']]);
			}else{
				msg('修改失败,写入数据库失败',-1);
			}
		}
		$artinfo = $artModel->where("id=$id")->get_one();
		$smarty->assign("artinfo", $artinfo);
		$smarty->assign('pageTitle', '修改活动');
		$smarty->assign('do', 'editArt');
		$smarty->display("admin/addArticle.html");
	break;
	case 'delArt':
		$id = intval(get_param('id'));
		if( empty($id) ) msg('非法id');
		$rs = $artModel->where("id=$id")->del();
		if( $rs ){
			ajax_return(array('status'=>'success', 'info'=>'删除成功'));
		}
		ajax_return(array('status'=>'failed', 'info'=>'删除失败'));
	break;
}
